package tests.cases {
	
	import mx.rpc.AsyncToken;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;
	
	import org.flexunit.Assert;
	
	import tests.cases.DefaultTestCase;
	
	public class TestRegister extends DefaultTestCase {
		
		private var coreSystemService:RemoteObject;
		
		[Before]
		public function setUp():void {
			
			coreSystemService = new RemoteObject();
			coreSystemService.destination = "amfphp";
			coreSystemService.source = "CoreSystemService";
		}
		
		[After]
		public function tearDown():void {
			
			coreSystemService = null;
		}
		
		// ----------------------------------------------------------------
		// TEST GOOD REGISTER
		// ----------------------------------------------------------------
		[Test(async)]
		public function testGoodRegister():void {
			
			var argsVO:Object = {
				firstName: "Unit",
				lastName: "Test User",
				email: "test@test.test",
				password: "password",
				phoneNumber: "0123456789",
				description: "User created by the unit testing process",
				gender: "M",
				dateOfBirth: "2009-06-12",
				location: "",
				occupation: "Test User",
				institution: "Open Groups"
			};
			
			var token:AsyncToken = coreSystemService.registerUser(
				argsVO.firstName, argsVO.lastName,
				argsVO.email, argsVO.password,
				argsVO.phoneNumber, argsVO.description,
				argsVO.gender,  argsVO.dateOfBirth,
				argsVO.location, argsVO.occupation, argsVO.institution
			);
			runAsyncTest(token, argsVO, handleGoodRegisterSuccess);
		}
		
		private function handleGoodRegisterSuccess(message:ResultEvent, passedData:Object):void {
			
			Assert.assertNotNull(message.result);
			
			Assert.assertNotNull(message.result.sessionID);
			Assert.assertNotNull(message.result.userData);
			Assert.assertNotNull(message.result.avatar);
			Assert.assertNotNull(message.result.theme);
			Assert.assertNotNull(message.result.groups);
		}
		
		// ----------------------------------------------------------------
		// TEST BAD REGISTER
		// ----------------------------------------------------------------
		[Test(async)]
		public function testBadRegister():void {
			
			var argsVO:Object = {
				firstName: "Unit",
				lastName: "Test User",
				email: "admin@test.com",
				password: "password",
				phoneNumber: "0123456789",
				description: "User created by the unit testing process",
				gender: "M",
				dateOfBirth: "2009-06-12",
				location: "",
				occupation: "Test User",
				institution: "Open Groups"
			};
			
			var token:AsyncToken = coreSystemService.registerUser(
				argsVO.firstName, argsVO.lastName,
				argsVO.email, argsVO.password,
				argsVO.phoneNumber, argsVO.description,
				argsVO.gender,  argsVO.dateOfBirth,
				argsVO.location, argsVO.occupation, argsVO.institution
			);
			runAsyncTest(token, argsVO, handleBadRegisterSuccess);
		}
		
		private function handleBadRegisterSuccess(message:ResultEvent, passedData:Object):void {
			
			Assert.assertNull(message.result);
		}
	}
}